package offer.diff;

/**
 * @author DengYuan2
 * @create 2021-04-20 9:07
 */
public class n_44_2 {
    public static void main(String[] args) {
        n_44_2 n = new n_44_2();
        int res = n.getDigitAtIndex(1245);
        System.out.println(res);

    }

    public int getDigitAtIndex(int index) {
        if (index<0){
            return -1;
        }
        //1代表个位，2代表十位......
        int place=1;
        while (true){
            int amount=getAmount(place);
            int total=amount*place;
            if (index<total){
                return digit(index,place);
            }
            index-=total;
            place++;
        }
    }

    public int getAmount(int place){
        if (place==1){
            return 10;
        }
        return (int) Math.pow(10,place-1)*9;
    }

    public int getBegin(int place){
        if (place==1){
            return 0;
        }
        return (int)Math.pow(10,place-1);
    }

    public int digit(int index,int place){
        int begin=getBegin(place);
        int shift=index/place;
        String num=(begin+shift)+"";
        int idx=index%place;
        return num.charAt(idx)-'0';
    }
}
